<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 2] 2.11 Operators</TITLE>
<META NAME="author" CONTENT="David Flanagan">
<META NAME="date" CONTENT="Thu Jul 31 15:48:01 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java in a Nutshell">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java in a Nutshell" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_10.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 2<br>How Java Differs from C</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch02_12.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JNUT2-CH-2-SECT-11">2.11 Operators</A></h2>

<P CLASS=para>
<A NAME="OPERATORS"></A><A NAME="PROGRAMMING-IN-JAVA2"></A>Java supports almost all of the standard C operators.  These
standard operators have the same precedence and
associativity in Java as they do in C.  They are listed in
<A HREF="ch02_11.htm#JNUT2-CH-2-TAB-3">Table 2.3</A>
and also in quick reference form in <A HREF="ch13_01.htm">Chapter 13, <i>Java Syntax</i></A>.

<P>
<DIV CLASS=table>
<TABLE BORDER>
<CAPTION><A CLASS="TITLE" NAME="JNUT2-CH-2-TAB-3">Table 2.3: Java Operators</A></CAPTION>
<TR CLASS=row>
<TH ALIGN="left">Prec.</TH>
<TH ALIGN="left">Operator</TH>
<TH ALIGN="left">Operand Type(s)</TH>
<TH ALIGN="left">Assoc.</TH>
<TH ALIGN="left">Operation Performed</TH>
</TR>
<TR CLASS=row>
<TD ALIGN="left">1</TD>
<TD ALIGN="left">++</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">pre-or-post increment </TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">(unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">--</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">pre-or-post decrement (unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">+, -</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">unary plus, unary minus</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">~</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">bitwise complement (unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">!</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">logical complement (unary)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">(<I CLASS=emphasis>type</I>)</TD>
<TD ALIGN="left">any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">cast</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">2</TD>
<TD ALIGN="left">*, /, %</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">multiplication, division, remainder</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">3</TD>
<TD ALIGN="left">+, -</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">addition, subtraction</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">+</TD>
<TD ALIGN="left">string</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">string concatenation</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">4</TD>
<TD ALIGN="left">&lt;&lt;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">left shift</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&gt;&gt;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">right shift with sign extension</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&gt;&gt;&gt;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">right shift with zero extension</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">5</TD>
<TD ALIGN="left">&lt;, &lt;=</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">less than, less than or equal</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&gt;, &gt;=</TD>
<TD ALIGN="left">arithmetic</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">greater than, greater than or equal</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">instanceof</TD>
<TD ALIGN="left">object, type</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">type comparison</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">6</TD>
<TD ALIGN="left">==</TD>
<TD ALIGN="left">primitive</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">equal (have identical </TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">values)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">!=</TD>
<TD ALIGN="left">primitive</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">not equal (have different values)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">==</TD>
<TD ALIGN="left">object</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">equal (refer to same object)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">!=</TD>
<TD ALIGN="left">object</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">not equal (refer to different objects)</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">7</TD>
<TD ALIGN="left">&amp;</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">bitwise AND</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">&amp;</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">boolean AND</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">8</TD>
<TD ALIGN="left">^</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">bitwise XOR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">^</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">boolean XOR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">9</TD>
<TD ALIGN="left">|</TD>
<TD ALIGN="left">integral</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">bitwise OR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">|</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">boolean OR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">10</TD>
<TD ALIGN="left">&amp;&amp;</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">conditional AND</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">11</TD>
<TD ALIGN="left">||</TD>
<TD ALIGN="left">boolean</TD>
<TD ALIGN="left">L</TD>
<TD ALIGN="left">conditional OR</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">12</TD>
<TD ALIGN="left">?:</TD>
<TD ALIGN="left">boolean, any, any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">conditional (ternary) operator</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">13</TD>
<TD ALIGN="left">

<P CLASS=para>
=</TD>
<TD ALIGN="left">variable, any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">assignment</TD>
</TR>
<TR CLASS=row>
<TD ALIGN="left">&nbsp;</TD>
<TD ALIGN="left">

<P CLASS=para>
*=, /=, %=, +=, -=, &lt;&lt;=, &gt;&gt;=, &gt;&gt;&gt;=, &amp;=, ^=, |=</TD>
<TD ALIGN="left">variable, any</TD>
<TD ALIGN="left">R</TD>
<TD ALIGN="left">assignment with operation</TD>
</TR>
</TABLE>
<P>
</DIV>
<P CLASS=para>
Note the following Java operator differences from C. Java
does not support the comma operator for combining two
expressions into one (although the <tt CLASS=literal>for</tt> statement
simulates this operator in a useful way).  
Since Java does not allow you to
manipulate pointers directly, it does not support the
reference and dereference operators <tt CLASS=literal>*</tt>, <tt CLASS=literal>-&gt;</tt>, and <tt CLASS=literal>&amp;</tt>,
nor the <tt CLASS=literal>sizeof</tt> operator.  Further, Java doesn't
consider <tt CLASS=literal>[]</tt> (array access) and <tt CLASS=literal>.</tt> (field
access) to be operators, as C does.

<P CLASS=para>
Java also adds some new operators:

<P CLASS=para>
The <tt CLASS=literal>+</tt> operator applied to <tt CLASS=literal>String</tt> values
concatenates them.
[5]
If only one operand of <tt CLASS=literal>+</tt> is a
<tt CLASS=literal>String</tt>, the other one is converted to a string.
The conversion is done automatically for primitive types,
and by calling the <tt CLASS=literal>toString()</tt> method of non-primitive
types.  This <tt CLASS=literal>String</tt> <tt CLASS=literal>+</tt> operator has the same
precedence as the arithmetic <tt CLASS=literal>+</tt> operator.  The
<tt CLASS=literal>+=</tt> operator works as you would expect for
<tt CLASS=literal>String</tt> values.

<blockquote class=footnote>
<P CLASS=para>[5] 
To C++ programmers, this looks like operator
overloading.  In fact, Java does not support operator
overloading--the language designers decided (after much
debate) that overloaded operators were a neat idea, but that
code that relied on them became hard to read and understand.
</blockquote>
<P CLASS=para>
The <tt CLASS=literal>instanceof</tt> operator returns <tt CLASS=literal>true</tt> if the
object <tt CLASS=literal>o</tt> on its left-hand side is an instance of the
class <tt CLASS=literal>C</tt> or implements the interface <tt CLASS=literal>I</tt>
specified on its right-hand side.  It also returns
<tt CLASS=literal>true</tt> if <tt CLASS=literal>o</tt> is an instance of a subclass of
<tt CLASS=literal>C</tt> or is an instance of a subclass of some class that
implements <tt CLASS=literal>I</tt>.  <tt CLASS=literal>instanceof</tt> returns
<tt CLASS=literal>false</tt> if <tt CLASS=literal>o</tt> is not an instance of <tt CLASS=literal>C</tt>
or does not implement <tt CLASS=literal>I</tt>.  It also returns
<tt CLASS=literal>false</tt> if the value on its left is <tt CLASS=literal>null</tt>.  If
<tt CLASS=literal>instanceof</tt> returns <tt CLASS=literal>true</tt>, it means that
<tt CLASS=literal>o</tt> is <I CLASS=emphasis>assignable to</I> variables of type <tt CLASS=literal>C</tt>
or <tt CLASS=literal>I</tt>.  The <tt CLASS=literal>instanceof</tt> operator has the same
precedence as the <tt CLASS=literal>&lt;</tt>, <tt CLASS=literal>&lt;=</tt>, <tt CLASS=literal>&gt;</tt>, and
<tt CLASS=literal>&gt;=</tt> operators.

<P CLASS=para>
Because all integral types in Java are signed values, the
Java <tt CLASS=literal>&gt;&gt;</tt> operator is defined to do a right shift with
sign extension.  The <tt CLASS=literal>&gt;&gt;&gt;</tt> operator treats the value
to be shifted as an unsigned number and shifts the bits
right with zero extension.  The <tt CLASS=literal>&gt;&gt;&gt;=</tt> operator works
as you would expect.

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><tt CLASS=literal>&amp;</tt> and <tt CLASS=literal>|</tt><br>
<DD>

<P CLASS=para>
When <tt CLASS=literal>&amp;</tt> and <tt CLASS=literal>|</tt> are applied to integral types
in Java, they perform the expected bitwise AND and OR
operations.  Java makes a strong distinction between
integral types and the <tt CLASS=literal>boolean</tt> type, however.  Thus,
if these operators are applied to <tt CLASS=literal>boolean</tt> types,
they perform logical AND and logical OR operations.  These
logical AND and logical OR operators always evaluate both of
their operands, even when the result of the operation is
determined after evaluating only the left operand.  This is
useful when the operands are expressions with side effects
(such as method calls) and you always want the side effects
to occur.  However, when you do not want the right operand
evaluated if it is not necessary, you can use the <tt CLASS=literal>&amp;&amp;</tt>
and <tt CLASS=literal>||</tt> operators, which perform "short-circuited" logical AND and
logical OR operations just as in C.  The <tt CLASS=literal>&amp;=</tt> and
<tt CLASS=literal>|=</tt> operators perform a bitwise or logical operation
depending on the type of the operands, as you would
expect.</DL>
</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_10.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch02_12.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Strings</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Statements</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
